// import { REQCATEGORY, USEMOCKBANNER, USEMOCKFLLOR } from "@/api";
import * as types from "../types";
import * as utils from "@/utils";
/**
 * home
 */

const state = {
  isLogin: false, // 是否登录
  loginDialogVisible: false, // 登录弹窗显示与隐藏
  isPlayed: false, // 当前播放状态
  playList: [], // 播放列表
  userInfo: null, // 登录用户信息
  playIndex: 0, // 当前播放歌曲在播放列表的所有位置
  isShowPlayListTips: false, // 添加及播放成功后，播放列表按钮提示的文字
};
const getters = {
  isLogin(state) {
    return state.isLogin;
  },
  isPlayed(state) {
    return state.isPlayed;
  },
  loginDialogVisible(state) {
    return state.loginDialogVisible;
  },
  userInfo(state) {
    return state.userInfo;
  },
  playList(state) {
    return state.playList;
  },
  playIndex(state) {
    return state.playIndex;
  },
  isShowPlayListTips(state) {
    return state.isShowPlayListTips;
  },
};
const mutations = {
  setLoginDialog(state, commit) {
    state.loginDialogVisible = commit;
  },
  ["SET_LOGIN"](state, val = false) {
    state.isLogin = val;
  },
  setUserInfo(state, val) {
    state.userInfo = val;
  },
  [types.SET_PLAYSTATUS](state, val = false) {
    state.isPlayed = val;
  },
  [types.SET_PLAYLIST](state, val = null) {
    state.playList = val;
  },
  [types.SET_PLAYINDEX](state, val = 0) {
    state.playIndex = val;
  },
  [types.SET_PLAYLISTTIPS](state, val = false) {
    state.isShowPlayListTips = val;
  },
};
const actions = {
  // 播放当前选中的歌曲
  selectPlay({ commit, state }, { list }) {
    const playList = concatPlayList(list, state.playList);

    commit(types.SET_PLAYLIST, playList);
    commit(types.SET_PLAYSTATUS, true);
    commit(types.SET_PLAYINDEX, findIndex(list[0], playList));
  },
  loginSuc(context, val) {
    context.commit("setLoginDialog", val);
  },
  // 播放歌曲列表里全部歌曲（清空当前播放列表）
  playAll({ commit }, { list }) {
    commit(types.SET_PLAYLIST, concatPlayList(list));
    commit(types.SET_PLAYSTATUS, true);
    commit(types.SET_PLAYINDEX, 0);
  },
  // 添加歌曲到当前播放列表
  addList({ commit, state }, { list }) {
    const playList = concatPlayList(list, state.playList);
    commit(types.SET_PLAYLIST, playList);
  },
};

export default {
  state,
  getters,
  mutations,
  actions,
};

// 当前歌曲在播放列表的索引
const findIndex = (list, playList) => {
  return playList.findIndex((d) => {
    return d.id === list.id;
  });
};
// 合并歌曲到播放列表查重
const concatPlayList = (list, playList = []) => {
  // filter过滤无版权及vip歌曲
  return utils.concatPlayList(
    list.filter((item) => !item.license && !item.vip),
    playList
  );
};
